package javagprssrv;

import javagprssrv.DataBase.Insert5mEachThread;
import javagprssrv.DataBase.UpdateHeartBeatThread;
import javagprssrv.StructClass.structDYB;
import org.dusin.commonutil.DateUtil;
import top.dusin.dyjccomm.test.Gen5mThread;

public class JGS_RUN
{
    public java.lang.String getCurrentDir() {
        java.lang.String ret = "";
        ret = new java.io.File("").getAbsolutePath();
        return ret;
    }
    
    public int Check() {
        int res = 0;
        javagprssrv.JGS_Param.Param_OS_Name = java.lang.System.getProperty("os.name");
        javagprssrv.JGS_Param.Param_OS_Version = java.lang.System.getProperty("os.version");
        javagprssrv.JGS_Param.Param_Java_Name = java.lang.System.getProperty("java.runtime.name");
        javagprssrv.JGS_Param.Param_Java_Version = java.lang.System.getProperty("java.version");
        javagprssrv.JGS_Param.Param_Java_JRE_Dir = java.lang.System.getProperty("java.home");
        final java.util.Properties p = java.lang.System.getProperties();
        final java.lang.String JRE_DIR = javagprssrv.JGS_Param.Param_Java_JRE_Dir;
        javagprssrv.JGS_Param.myLog.ShowLog("S T E P 1  、  检查系统配置参数", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("检查(1.1) -> 操作系统版本 = " + java.lang.System.getProperty("os.name") + " ( " + java.lang.System.getProperty("os.version") + " )", true, true);
        javagprssrv.JGS_Param.myLog.ShowLog("检查(1.2) -> Java 版  本 = " + javagprssrv.JGS_Param.Param_Java_Name + " ( " + javagprssrv.JGS_Param.Param_Java_Version + " )", true, true);
        javagprssrv.JGS_Param.myLog.ShowLog("检查(1.3) -> Java安装目录 = " + JRE_DIR, true, true);
        javagprssrv.JGS_Param.myLog.ShowLog("检查(2.1) -> 当前目录 = " + this.getCurrentDir(), true, true);
        java.lang.String is_GPRS_INI_CZ = "";
        final java.io.File f = new java.io.File("GPRS.properties");
        if (!f.exists()) {
            is_GPRS_INI_CZ = "不存在，请检查“" + this.getCurrentDir() + "”是否为工作目录！";
            javagprssrv.JGS_Param.myLog.ShowLog("检查(3.1) -> 读取GPRS.properties = " + is_GPRS_INI_CZ, true, true);
            res = 100;
            return res;
        }
        is_GPRS_INI_CZ = "完成";
        javagprssrv.JGS_Param.myLog.ShowLog("检查(3.1) -> 读取GPRS.properties = " + is_GPRS_INI_CZ, true, true);
        javagprssrv.JGS_Param.myLog.ShowLog("S T E P 2  、  检查运行环境", true, false);
        java.lang.String is_JDBC_API_CZ = "";
        java.io.File f_JDBC_API = new java.io.File(JRE_DIR + "\\lib\\ext\\mssqlserver.jar");
       
        is_JDBC_API_CZ = "完成";
        javagprssrv.JGS_Param.myLog.ShowLog("检查(4.1.3) -> SQLServer JDBC API(msutil.jar) = " + is_JDBC_API_CZ, true, true);
        java.lang.String is_Java_Comm_CZ = "";
        java.io.File f_JavaComm = new java.io.File(JRE_DIR + "\\lib\\javax.comm.properties");
        if (!f_JavaComm.exists()) {
            is_Java_Comm_CZ = "不存在，请检查" + JRE_DIR + "\\lib目录下是否有'javax.comm.properties'！";
            javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.1) -> Java Comm API(javax.comm.properties) = " + is_Java_Comm_CZ, true, true);
            //res = 100;
            //return res;
        }
        is_Java_Comm_CZ = "完成";
        javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.1) -> Java Comm API(javax.comm.properties) = " + is_Java_Comm_CZ, true, true);
        is_Java_Comm_CZ = "";
        f_JavaComm = new java.io.File(JRE_DIR + "\\lib\\ext\\comm.jar");
        if (!f_JavaComm.exists()) {
            is_Java_Comm_CZ = "不存在，请检查" + JRE_DIR + "\\lib\\ext目录下是否有'comm.jar'！";
            javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.2) -> Java Comm API(comm.jar) = " + is_Java_Comm_CZ, true, true);
            //res = 100;
            //return res;
        }
        is_Java_Comm_CZ = "完成";
        javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.2) -> Java Comm API(comm.jar) = " + is_Java_Comm_CZ, true, true);
        is_Java_Comm_CZ = "";
        f_JavaComm = new java.io.File(JRE_DIR + "\\bin\\win32com.dll");
        if (f_JavaComm.exists()) {
            is_Java_Comm_CZ = "完成";
            javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.3) -> Java Comm API(win32com.dll) = " + is_Java_Comm_CZ, true, true);
            return res;
        }
        is_Java_Comm_CZ = "不存在，请检查" + JRE_DIR + "\\bin目录下是否有'win32com.dll'！";
        javagprssrv.JGS_Param.myLog.ShowLog("检查(4.2.3) -> Java Comm API(win32com.dll) = " + is_Java_Comm_CZ, true, true);
        res = 100;
        return res;
    }
    
    private void ShowStart() {
        final javagprssrv.Common.DateTime DT = new javagprssrv.Common.DateTime();
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("  系统名称 :     " + javagprssrv.JGS_Param.AppName, true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("  系统版本 :     " + javagprssrv.JGS_Param.AppVersion, true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("  启动时间 :     " + DT.getDateTime("YYYY-MM-DD HH:NN:SS"), true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("  注    意 :  本系统建议在 JRE 1.6.0_17 (或更高版本) 中运行 ！ ", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
        javagprssrv.JGS_Param.myLog.ShowLog("", true, false);
    }
    
    private void CommStart() {
    }
    
    private void SocketStart() {
        
        
        if (JGS_Param.properties.getProperty("即时数据监听端口打开").equals("1")) {
            int SocketControl = -1;
            java.lang.String SocketProto = "";
            java.lang.String s = JGS_Param.properties.getProperty("即时数据监听端口");
            while (s.indexOf(":") >= 0) {
                java.lang.String ProtocalAndPort = "";
                if (s.indexOf(",") > -1) {
                    ProtocalAndPort = s.substring(0, s.indexOf(","));
                }
                else {
                    ProtocalAndPort = s;
                }
                if (ProtocalAndPort.length() < 5 || ProtocalAndPort.indexOf(":") == -1) {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                }
                else {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                    SocketProto = ProtocalAndPort.substring(0, 3).toUpperCase();
                    SocketControl = java.lang.Integer.valueOf(ProtocalAndPort.substring(4, ProtocalAndPort.length()));
                    final javagprssrv.Listener.TrdJSSJDataListener TJSSJDL = new javagprssrv.Listener.TrdJSSJDataListener(SocketProto, SocketControl, "即时数据监听端口");
                    TJSSJDL.start();
                }
            }
        }
        if (JGS_Param.properties.getProperty( "控制端口打开").equals("1")) {
            int SocketControl = -1;
            java.lang.String SocketProto = "";
            java.lang.String s =JGS_Param.properties.getProperty("控制端口");
            while (s.indexOf(":") >= 0) {
                java.lang.String ProtocalAndPort = "";
                if (s.indexOf(",") > -1) {
                    ProtocalAndPort = s.substring(0, s.indexOf(","));
                }
                else {
                    ProtocalAndPort = s;
                }
                if (ProtocalAndPort.length() < 5 || ProtocalAndPort.indexOf(":") == -1) {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                }
                else {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                    SocketProto = ProtocalAndPort.substring(0, 3).toUpperCase();
                    SocketControl = java.lang.Integer.valueOf(ProtocalAndPort.substring(4, ProtocalAndPort.length()));
                    final javagprssrv.Listener.TrdControlListener TSS = new javagprssrv.Listener.TrdControlListener(SocketProto, SocketControl, "控制端口");
                    TSS.start();
                }
            }
        }
        if (JGS_Param.properties.getProperty("仪表监听端口打开").equals("1")) {
            int SocketControl = -1;
            java.lang.String SocketProto = "";
            java.lang.String s = JGS_Param.properties.getProperty("仪表监听端口");
            while (s.indexOf(":") >= 0) {
                java.lang.String ProtocalAndPort = "";
                if (s.indexOf(",") > -1) {
                    ProtocalAndPort = s.substring(0, s.indexOf(","));
                }
                else {
                    ProtocalAndPort = s;
                }
                if (ProtocalAndPort.length() < 5 || ProtocalAndPort.indexOf(":") == -1) {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                }
                else {
                    if (s.indexOf(",") > -1) {
                        s = s.substring(s.indexOf(",") + 1, s.length());
                    }
                    else {
                        s = "";
                    }
                    SocketProto = ProtocalAndPort.substring(0, 3).toUpperCase();
                    SocketControl = java.lang.Integer.valueOf(ProtocalAndPort.substring(4, ProtocalAndPort.length()));
                    final javagprssrv.Listener.TrdYiBiaoDataListener TYBDL = new javagprssrv.Listener.TrdYiBiaoDataListener(SocketProto, SocketControl, "仪表监听端口");
                    TYBDL.start();
                }
            }
        }
        if (JGS_Param.properties.getProperty("输出端口打开").equals("1")) {
            int SocketControl = -1;
            java.lang.String SocketProto = "";
            java.lang.String RemoteIP = "";
            java.lang.String s2 = JGS_Param.properties.getProperty("输出端口");
            while (s2.indexOf(":") >= 0) {
                java.lang.String ProtocalAndPort2 = "";
                if (s2.indexOf(",") > -1) {
                    ProtocalAndPort2 = s2.substring(0, s2.indexOf(","));
                }
                else {
                    ProtocalAndPort2 = s2;
                }
                if (ProtocalAndPort2.length() < 5 || ProtocalAndPort2.indexOf(":") == -1) {
                    if (s2.indexOf(",") > -1) {
                        s2 = s2.substring(s2.indexOf(",") + 1, s2.length());
                    }
                    else {
                        s2 = "";
                    }
                }
                else {
                    if (s2.indexOf(",") > -1) {
                        s2 = s2.substring(s2.indexOf(",") + 1, s2.length());
                    }
                    else {
                        s2 = "";
                    }
                    SocketProto = ProtocalAndPort2.substring(0, 3).toUpperCase();
                    SocketControl = java.lang.Integer.valueOf(ProtocalAndPort2.substring(4, ProtocalAndPort2.length()));
                    RemoteIP = JGS_Param.properties.getProperty("输出IP");
                    javagprssrv.JGS_Param.OutputSocket = new javagprssrv.StructClass.structOutputSocket(SocketProto, RemoteIP, SocketControl);
                }
            }
        }
    }
    
    private int InitDataBase() {
        final int res = 1;
        return res;
    }
    
    public void InitTerms() {
        javagprssrv.JGS_Param.Terms.clear();
        javagprssrv.JGS_Param.myLog.ShowLog("InitTerms() 顺利完成 ！！！", java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
    }
    
    public void Run() {
        javagprssrv.JGS_Param.AppStartTime = DateUtil.get();
        javagprssrv.JGS_Param.TaskCount = 0;
        this.ShowStart();
        final int res = this.Check();
        if (res != 0) {
            java.lang.System.exit(res);
        }
        javagprssrv.JGS_Param.COMMTerms.clear();
        javagprssrv.JGS_Param.UnDoneCOMMTasks.clear();
        javagprssrv.JGS_Param.Tasks.clear();
        javagprssrv.JGS_Param.ClientPipe.clear();
        javagprssrv.JGS_Param.myLog.ShowLog("S T E P 3  、  检查系统所用串口", true, false);
        new javagprssrv.Serial.SerialDetect().DetectCOM();
        javagprssrv.JGS_Param.myLog.ShowLog("S T E P 4  、  初始化", true, false);
     
        new javagprssrv.DataAnalyse.TrdDataAnalyse().start();
        for (int _TrdDataAnalyse_DBAction_Count = javagprssrv.JGS_Param._TrdDataAnalyse_DBAction_Count, i = 0; i < _TrdDataAnalyse_DBAction_Count; ++i) {
            new javagprssrv.DataAnalyse.TrdDataAnalyse_DBAction().start();
        }
        javagprssrv.JGS_Param.myLog.ShowLog("S T E P 5  、  检查系统所用端口", true, false);
        this.SocketStart();
        new javagprssrv.Task.TrdTaskManager().start();
        new javagprssrv.Task.TaskTimeOutManager().start();
        new javagprssrv.DataBase.TrdDBSQLRun().start();
        new javagprssrv.DataBase.Insert5mThread().start();
        new Insert5mEachThread().start();
        new javagprssrv.Task.TrdGuanLi().start();
        new DT3.TrdUDPDataSaver().start();
        new javagprssrv.DataAnalyse.TrdTCPDataSaver().start();
        new NBJL.TrdTCPDataSaver_NBJL().start();
        new DT8.TrdTCPDataSaver_DT8().start();
        new DT42.TrdTCPDataSaver_DT42().start();
        //new Gen5mThread().start();
        new UpdateHeartBeatThread().start();
        //javagprssrv.JGS_Param.UnSavedDBSQLString.add("insert into t_5m(deviceID,pointID,DataTime,voltage) values(111,111,'2022-01-01',666)");//测试用
        //javagprssrv.JGS_Param.queueFor5m.add(new Object[]{111,111,"1975-01-01",666});
        //javagprssrv.JGS_Param.queueFor5m.add(new Object[]{222,222,"1975-01-01 12:05:00",666});
        //DT42.TrdTCPDataSaver_DT42 DT42=new DT42.TrdTCPDataSaver_DT42();
        //DT42.start();
        
        for (int i = 0; i < javagprssrv.JGS_Param.COMMTerms.size() + 2; ++i) {
            new javagprssrv.Task.TaskExecutor().start();
        }
        new javagprssrv.Timer.TimersStarter().TimersStart();
        try {
            java.lang.Thread.sleep(3000L);
        }
        catch (java.lang.Exception ex) {}
        javagprssrv.JGS_Param.myLog.ShowLog("           系统已经完成所有准备,开始运行 ！", java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
        javagprssrv.JGS_Param.isSystemReady = true;
    } 
  
}
